﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebEntities;
using WebEntities.Models.WebInfo;

namespace com.subb.WebInfo
{
    public class WI_IndexAdImgDAL
    {
        public List<WI_IndexADImg> GetAllADImages(int start, int limit, out int ttlCount)
        {
            ttlCount = 0;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.WI_IndexADImg.OrderBy(c => c.SortNum ?? 0).ThenBy(c => c.ImgID);
                ttlCount = query.Count();
                List<WI_IndexADImg> imglist = query.Skip(start).Take(limit).ToList();
                return imglist;
            }
        }

        public List<WI_IndexADImg> GetAllADImages()
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.WI_IndexADImg.OrderBy(c => c.SortNum ?? 0).ThenBy(c => c.ImgID);
                List<WI_IndexADImg> imglist = query.ToList();
                return imglist;
            }
        }

        public WI_IndexADImg GetADImg(int id)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_IndexADImg adimg = ctx.WI_IndexADImg.FirstOrDefault(c => c.ImgID == id);
                return adimg;
            }
        }

        public int CreateADImg(string imgUrl, int sortNum, string imgName)
        {
            int retId = 0;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_IndexADImg adimg = new WI_IndexADImg
                {
                    ImgUrl = imgUrl,
                    ImgName = imgName,
                    SortNum = sortNum
                };
                ctx.WI_IndexADImg.AddObject(adimg);
                ctx.SaveChanges();
                retId = adimg.ImgID;
            }
            return retId;
        }

        public bool UpdateADImg(int id, string imgUrl, int sortNum, string imgName)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_IndexADImg adimg = ctx.WI_IndexADImg.FirstOrDefault(c => c.ImgID == id);
                if (adimg != null)
                {
                    adimg.ImgUrl = imgUrl;
                    adimg.ImgName = imgName;
                    adimg.SortNum = sortNum;
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }

        public bool RemoveADImg(int id)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_IndexADImg adimg = ctx.WI_IndexADImg.FirstOrDefault(c => c.ImgID == id);
                if (adimg != null)
                {
                    ctx.WI_IndexADImg.DeleteObject(adimg);
                    ctx.SaveChanges();
                    result = true;
                }
                return result;
            }
        }
    }
}
